package com.ruipengkj.commons.util;

import java.awt.Component;

import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class TabelUtil {
	/**获取某列的宽度*/
	public static int getPreferredWidthForCloumn(JTable table, int icol) {
		TableColumnModel tcl = table.getColumnModel();
		TableColumn col = tcl.getColumn(icol);
		int c = col.getModelIndex(), width = 0, maxw = 0;
		for (int r = 0; r < table.getRowCount(); ++r) {
			TableCellRenderer renderer = table.getCellRenderer(r, c);
			Component comp = renderer.getTableCellRendererComponent(table,
					table.getValueAt(r, c), false, false, r, c);
			width = comp.getPreferredSize().width;
			maxw = width > maxw ? width : maxw;
		}
		return maxw;
	}
	/**设置某列的宽度*/
	public static void setTableColumnWidths(JTable table, int column, int width) {
		TableColumn firsetColumn = table.getColumnModel().getColumn(column);
		firsetColumn.setPreferredWidth(width);
//		firsetColumn.setMaxWidth(width);
//		firsetColumn.setMinWidth(width);
	}

	/** 设置JTabel自动列宽 */
	public static void setJTabelAutoClumnWidth(JTable table) {
		for (int i = 0; i < table.getColumnCount(); i++) {
			int with = getPreferredWidthForCloumn(table, i) + 50;
			table.getColumnModel().getColumn(i).setPreferredWidth(with);
		}
	}
	/**隐藏指定列*/
	public static void hideTableColumn(int index, JTable table) {
		TableColumn tc = table.getColumnModel().getColumn(index);
		tc.setMaxWidth(0);
		tc.setPreferredWidth(0);
		tc.setWidth(0);
		tc.setMinWidth(0);
		table.getTableHeader().getColumnModel().getColumn(index).setMaxWidth(0);
		table.getTableHeader().getColumnModel().getColumn(index).setMinWidth(0);
	}
	/**设置JTabel自动列宽*/
	public static void adjustTableColumnWidths(JTable table) {
		JTableHeader header = table.getTableHeader(); // 表头
		int rowCount = table.getRowCount(); // 表格的行数
		TableColumnModel cm = table.getColumnModel(); // 表格的列模型
		for (int i = 0; i < cm.getColumnCount(); i++) { // 循环处理每一列
			TableColumn column = cm.getColumn(i); // 第i个列对象
			int width = (int) header.getDefaultRenderer()
					.getTableCellRendererComponent(table,
							column.getIdentifier(), false, false, -1, i)
					.getPreferredSize().getWidth(); // 用表头的绘制器计算第i列表头的宽度
			for (int row = 0; row < rowCount; row++) { // 循环处理第i列的每一行，用单元格绘制器计算第i列第row行的单元格宽度
				int preferedWidth = (int) table.getCellRenderer(row, i)
						.getTableCellRendererComponent(table,
								table.getValueAt(row, i), false, false, row, i)
						.getPreferredSize().getWidth();
				width = Math.max(width, preferedWidth); // 取最大的宽度
			}
			column.setPreferredWidth(width + table.getIntercellSpacing().width); // 设置第i列的首选宽度
		}
		table.doLayout(); // 按照刚才设置的宽度重新布局各个列
	}
	/** 清除JTable数据*/
	@SuppressWarnings("unchecked")
	public static synchronized void clearTableRowSorter(JTable table) {
		RowSorter rowSort = table.getRowSorter();
		if (rowSort != null) {
			rowSort.setSortKeys(null);// 如果使用了排序器，先清除对表的排序
		}
	}
}
